import sys

from PyQt6.QtCore import Qt, QSize, QEventLoop, QTimer
from PyQt6.QtGui import QIcon, QGuiApplication, QPixmap, QPalette, QBrush, QGradient
from PyQt6.QtWidgets import QFrame, QMainWindow, QApplication, QHBoxLayout, QPushButton, QVBoxLayout, QWidget, QLayout
from qfluentwidgets import SplashScreen
from qfluentwidgets.components.widgets.frameless_window import FramelessWindow


class ToolsWindow(FramelessWindow):

    def __init__(self):
        # 调用父类的初始化方法
        super().__init__()

        # 1. 创建启动页面
        self.splashScreen = SplashScreen(QIcon('./static/Icon.png'), self)
        self.splashScreen.setIconSize(QSize(102, 102))

        # 3. 创建子界面
        self.createSubInterface()
        # self.splashScreen.show()

        # 初始化 UI
        self.init_ui()

        # 4. 隐藏启动页面
        self.splashScreen.finish()

    def createSubInterface(self):
        loop = QEventLoop(self)
        QTimer.singleShot(30000, loop.quit)
        loop.exec()

    def init_ui(self):
        print("窗口绘制处理")
        self.setWindowTitle('个人工具')
        self.setWindowIcon(QIcon('./static/Icon.png'))
        self.setGeometry(0, 0, 800, 500)

        # 窗口居中
        self.center_position()

        centerWidget = QWidget()
        self.setCentralWidget(centerWidget)

        # 设置窗口区域
        # 通过选项卡实现功能切换
        # 创建两个用于显示控件的QWidget对象
        self.hLay = QHBoxLayout()

        # 菜单位置
        self.menuLay = QVBoxLayout()
        self.menuLay.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.hLay.addLayout(self.menuLay, 1)

        # 展示位置
        self.viewLay = QVBoxLayout()
        self.hLay.addLayout(self.viewLay, 4)

        self.centralWidget().setLayout(self.hLay)

    def center_position(self):
        print("窗口置中处理")
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def add_menu_item(self, item: QPushButton):
        print("增加菜单项:", item.text())
        self.menuLay.addWidget(item)

    def remove_menu_item(self, item: QPushButton):
        print("移除菜单项")
        self.menuLay.removeItem(item)

    def _clear_display_area(self):
        print("展示区域清空:", self.viewLay.count())
        # 移除旧的数据
        while self.viewLay.count() > 0:
            i = self.viewLay.itemAt(0)
            print("展示区域清空中：", i)
            self.viewLay.removeItem(i)
            # 关闭对象
            i.widget().close()
            print("展示区域清理结束：", self.viewLay.count())

    def set_display_area(self, a0: QWidget):
        print("展示区域设置")

        # 清空展示区域
        self._clear_display_area()

        # 添加新的数据
        self.viewLay.addWidget(a0)
